<template>
	<view>
		<button v-show="isLogin" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber " class="content"></button>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted,
		defineEmits,
		computed,
		onUpdated,
		watch
	} from "vue";
	import {
		onShow
	} from '@dcloudio/uni-app';
	import {
		Login
	} from "../../requestApi/api.js";
	import {
		useTokenState
	} from "@/store/index";
	const emit = defineEmits(['showTab']);
	const tokenState = useTokenState(); //token状态
	// const data = ref({
	// 	isLogin: uni.getStorageSync('token')?false:true,  //有token表示登陆过 不需要在登录  没得token表示需要授权登录
	// })
	const isLogin = computed(() => {
		let state = tokenState.getToken();
		emit('showTab', state);
		if (state) {
			uni.hideTabBar();
		} else {
			uni.showTabBar();
		}
		return state;
	})

	onMounted(() => {

	});


	function getPhoneNumber(e) {
		console.log(e)
		let form = {};
		if (e.detail.code) {
			form.encryptedData = e.detail.encryptedData;
			form.iv = e.detail.iv;
			uni.login({
				success: (r) => {
					form.code = r.code;
					uni.getUserInfo({
						success: (res) => {
							form.rawData = res.rawData;
							form.signature = res.signature;
							Login(form).then(res => {
								console.log(res)
								uni.setStorageSync('userInfo', res.data);
								uni.setStorageSync('token', res.data.token);
								tokenState.getToken();
								// emit('showTab', false);
								uni.showTabBar();
							})
						}
					})

				}
			})

		}

	};
</script>

<style lang="scss">
	.content {
		position: fixed;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: rgba(0, 0, 0, 0);
		z-index: 999;
	}
</style>